<?php
namespace app\admin\controller;

// 公共类
use app\common\model\Order;
use app\common\model\Vcode;
use plug\SUtil;
use think\Controller;
use think\Db;

class Base extends Controller
{
    public $_userid;
    public $_user_name;
    public $_groupid;
    public $_adminArr;
    public $_licenseid;
    public $_shopid;
    public $limit = 10;

    public function __construct()
    {
        parent::__construct();
        header("Content-type:text/html;charset=utf-8");
        $this->loadLoginUser();
        $this->assign("thisadmin",$this->_user_name);
    }



    function post($key, $type = 'string', $default=NULL)
    {
        $value = SUtil::getStr($_POST[$key], $type, $default);
        if (is_null($default) && is_null($value)) {
            $this->printJson(array('code' => -1, 'stats' => 'fail', 'msg' => '缺少参数'));
        }
        return $value;
    }

    function ajpost($key, $type = 'string', $default=NULL) {
        $value = SUtil::getStr($_POST[$key], $type, $default);
        if (is_null($default) && is_null($value)) {
            $this->outData['method'] = 'alert';
            $this->outData['msg'] = '请填写必填项';
            $this->printJson();
        }
        return $value;
    }

    function regpost($key, $type = 'string', $default=NULL)
    {
        $value = SUtil::getStr(input($key), $type, $default);
        if (is_null($default) && is_null($value)) {
            $this->result('','-1','请填写必填项');
        }
        return $value;
    }

    // 获取值
    function get($key, $type = 'string', $default = NULL)
    {
        $value = SUtil::getStr($_GET[$key], $type, $default);
        if (is_null($default) && is_null($value)) {
            $this->showMsg("请填写必填项");
        }
        return $value;
    }


    // 验证是否登录
    function isLogin()
    {
        if (!$this->_userid || !$this->_user_name) {
            $this->redirect('/login');
            exit;
        }
        return true;
    }

    // 定义常量赋值登录的账号信息
    function loadLoginUser()
    {
        // 获取cookie
        $cookie = cookie('name');
        if ($cookie) {
            $adminModel = new \app\common\model\Admin();
            $adminArr = $adminModel->getOne(array("user_id" => $cookie['admin_userid']));
            if($adminArr){
                $group = Db::table('ddt_group')->where('group_id', $adminArr['group_id'])->field('license_data')->find();
                $this->_licenseid = $group;
                $this->_user_name = $adminArr['user_name'];
                $this->_userid = $adminArr['user_id'];
                $this->_groupid = $adminArr['group_id'];
                $this->_adminArr = $adminArr;
                $this->_shopid = $adminArr['shop_id'];
            }
        }
    }

    // 判断是否总部
    function isHqAdmin()
    {
        if($this->_shopid == 0){
            return true;
        }
        return false;
    }

    function excelExport($fileName = '', $headArr = [], $data = []) {

        $fileName .= "-" . date("YmdHi", time()) . ".xls";

        $objPHPExcel = new \PHPExcel();
//        print_r($objPHPExcel);die;
        $objPHPExcel->getProperties();

        $key = ord("A"); // 设置表头

        foreach ($headArr as $v) {

            $colum = chr($key);

            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

            $key += 1;

        }

        $column = 2;

        $objActSheet = $objPHPExcel->getActiveSheet();

        foreach ($data as $key => $rows) { // 行写入

            $span = ord("A");

            foreach ($rows as $keyName => $value) { // 列写入

                $objActSheet->setCellValue(chr($span) . $column, $value);

                $span++;

            }

            $column++;

        }

        $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表

        $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表

        header('Content-Type: application/vnd.ms-excel');

        header("Content-Disposition: attachment;filename=$fileName");

        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output'); // 文件通过浏览器下载

        exit();

    }


}
